<!DOCTYPE html>
<html>
<head>
    <title>地址列表</title>
    <include file='public/head'>
	<style>
		.layui-legend-size{font-size:14px !important;}
		.layui-nopadding{padding:0 !important;}
		.layui-overflow{overflow:auto;}
		.layui-nomargin{margin: 0 !important;}
		.layui-bottom{border-bottom:1px solid rgb(226, 226, 226);}
		.layui-input{height:30px !important; }
		.layui-form-search-item .layui-inline{margin-bottom: 5px;} 
		.layui-form-search-item label{margin-left: 15px;}
		.layui-table-page {padding: 7px 0px 0px;}
		.layui-field-padding{border-width: 1px;padding:0 0 5px 10px;}
	</style>
</head>
<body>
<div class="layui-tab " lay-filter="demo-tab">
	<!-- 顶部导航菜单开始 -->
	<div class="layui-tab-nav" >
        <ul class="layui-tab-title">
			{:getNav('address','admin_index/index')};
		</ul>
	</div>
	<!-- 表格内容开始 -->
	<div class="layui-card" style="margin-top:20px">
		
		<div class="layui-card-header layui-bg-gray layui-bottom">

			<a class="layui-btn layui-btn-xs layui-btn-danger" onclick="sort()">排序</a>
            <a class="layui-btn layui-btn-xs " onclick="addSubord({$fa_id})">当前页添加分类</a>
			<a class="layui-btn layui-btn-xs "  href="{:cmf_plugin_url('Address://admin_index/Subord')}?cri_superior_code={$fa_code}">返回上级</a>
		</div>
		<div class="layui-card-body layui-nopadding">
			<form  id="dara" >
			<table class="layui-table layui-nomargin">
				<thead class="well">
					<tr>  
						<th style="text-align:center;width:5%;">ID</th>
						<th style="text-align:center;width:10%;">排序</th>
						<th style="text-align:center;">地区名称</th>
						<th style="text-align:center;">地区简称</th>
						<th style="text-align:center;">地区描述</th>
						<th style="text-align:center;">是否显示</th>
						<th style="text-align:center;">操作</th>
					</tr>
				</thead>
				<tbody>
					<volist name="data" id="vo">
					<tr>
						<td >{$vo.cri_id}</td>
						<td >
							<input type="text" id="{$vo.cri_id}"  style="width:40%;text-align:center;" value="{$vo.cri_sort}" oninput = "value=value.replace(/[^\d]/g,'')">
						</td>
						<td >{$vo.cri_name}</td>
						<td >{$vo.cri_short_name}</td>
						<td >{$vo.cri_memo}</td> 	
						<td>
							<switch name="vo.status">
								<case value="1">
									<a style="text-decoration:none;color:green;"  class=" js-ajax-delete" data-msg="确定变更状态为隐藏吗？" href="{:cmf_plugin_url('Address://admin_index/turn_off')}?cri_id={$vo.cri_id}" >√</a>
								</case>
								<case value="2">
									<a style="text-decoration:none;color:red;"  class=" js-ajax-delete" data-msg="确定变更状态为显示吗？" href="{:cmf_plugin_url('Address://admin_index/turn_on')}?cri_id={$vo.cri_id}" >×</a>
								</case>
							</switch>	
						</td>
						<td width="350">
							<if condition="$vo.subord eq 1">
								<a class="layui-btn layui-btn-xs "  href="{:cmf_plugin_url('Address://admin_index/Subord')}?cri_superior_code={$vo.cri_code}">查看下级地区</a>|
							</if>
							<a class="layui-btn layui-btn-xs " onclick="addSubord({$vo.cri_id})">添加下级地区</a>
							|
							<a class="layui-btn layui-btn-xs " onclick="editSubord({$vo.cri_id})">编辑</a>
							|
							<a class="layui-btn layui-btn-xs layui-btn-danger btn-delete" data-id="{$vo.cri_id}" 
							href="javascript:;" >删除</a>
						</td>
					</tr>
				</volist>
				</tbody>
				<!-- 表格内容输出结束 -->
			</table>
			</form>
		</div>
	</div>
</div>

<include file="public/scripts"/>

<script type="text/javascript">
	
$(function(){

//删除
$('.btn-delete').on('click',function(){
	let cri_id = $(this).data('id');
	let text = "确定要删除吗?";
	let url = "{:cmf_plugin_url('Address://admin_index/del')}?cri_id="+cri_id;

	layer.confirm(text, {icon: 3, title:'提示', btn: ['删除','取消']}, function(index,layero){
        $.ajax({
			url: url,
			success:function(data){
				if(data.code==1){
	        		layer.msg(data.msg,{icon:1,time:1500},function(){
						window.location.reload();
	                });
	        	}else{
	            	layer.msg(data.msg,{icon:2,time:1500});
	        	}
			},
			error:function(){
				layer.msg('系统错误！',{icon:2,time:1500});
			}
		})
    },function(index){
        // alert('取消');
    });
});


})

</script>
<script>
//页面层
function addSubord(data){
	layer.open({
	title:"添加下级地区",
	closeBtn: 1, //不显示关闭按钮
    type: 2,
    offset: '150px',
    btn:['提交'],
	// type: 1,
	// skin: 'layui-layer-rim', //加上边框
	area: ['400px', '380px'], //宽高
	content: "{:cmf_plugin_url('Address://admin_index/addPage')}?cri_id="+data,
	// content: $('#addMember')
	yes:function(index, layero){
        var body = layer.getChildFrame('body', index);
        var dara = body.find('#da').serialize();
        var name = body.find('input[name=cri_name]').val();
        var code = body.find('input[name=cri_code]').val();
        var short_name = body.find('input[name=cri_short_name]').val();

        if(name==''){
            layer.msg('地区名称不可为空', function(){
			});
            return false;
        }
        if(short_name==''){
            layer.msg('地区简称不可为空', function(){
			});
            return false;
        }
        if(code==''){
            layer.msg('地区代码不可为空', function(){
			});
            return false;
        }
        $.ajax({
            url:"{:cmf_plugin_url('Address://admin_index/add')}",
            data:dara,
            success:function(data){
                if(data.type=='success'){
            		layer.msg(data.msg,{time:2000},function(){
	                    layer.closeAll();
						window.location.reload();
	                });
            	}else if(data.type=='error'){
                	layer.msg(data.msg);
            	}
            },
            error:function(data){
                layer.msg('添加失败');
                return false;
            }
        })
    }
	});
}
</script>
<script>
//页面层
function editSubord(data){
	layer.open({
	title:"编辑地区信息",
	closeBtn: 1, //不显示关闭按钮
    type: 2,
    offset: '150px',
    btn:['提交'],
	// type: 1,
	// skin: 'layui-layer-rim', //加上边框
	area: ['400px', '380px'], //宽高
	content: "{:cmf_plugin_url('Address://admin_index/editPage')}?cri_id="+data,
	yes:function(index, layero){
        var body = layer.getChildFrame('body', index);
        var dara = body.find('#da').serialize();
        var name = body.find('input[name=cri_name]').val();
        var code = body.find('input[name=cri_code]').val();
        var short_name = body.find('input[name=cri_short_name]').val();

        if(name==''){
            layer.msg('地区名称不可为空', function(){
			});
            return false;
        }
        if(short_name==''){
            layer.msg('地区简称不可为空', function(){
			});
            return false;
        }
        if(code==''){
            layer.msg('地区代码不可为空', function(){
			});
            return false;
        }
        $.ajax({
            url:"{:cmf_plugin_url('Address://admin_index/edit')}",
            data:dara,
            success:function(data){
                if(data.type=='success'){
            		layer.msg(data.msg,{time:2000},function(){
	                    layer.closeAll();
						window.location.reload();
	                });
            	}else if(data.type=='error'){
                	layer.msg(data.msg);
            	}
            },
            error:function(data){
                layer.msg('添加失败');
                return false;
            }
        })
    }
	});
}
</script>
<script>
//页面层
function sort(){
	var list = $('#dara').find("input[type='text']");
	var data=new Array();
	var key=new Array();
	var value=new Array();
	for(var i=0;i<list.length && list[i];i++)
	{
		key[i] = list[i].id;
		value[i] = list[i].value;
	}
	$.ajax({
	    url:"{:cmf_plugin_url('Address://admin_index/sort')}?key="+key+'&value='+value,
	    data:data,
	    success:function(data){
	        if(data.type=='success'){
			window.location.reload();
	    	}else if(data.type=='error'){
	        	layer.msg('排序失败');
	    	}
	    },
	    error:function(data){
	        layer.msg('排序失败');
	        return false;
	    }
	})

}
</script>

</body>
